package com.xm9m.ibczy.mytest.alimama.mobile.demo;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.alimama.mobile.sdk.MmuSDK;
import com.alimama.mobile.sdk.config.FeedController;
import com.alimama.mobile.sdk.config.FeedProperties;
import com.alimama.mobile.sdk.config.MMPromoter;
import com.alimama.mobile.sdk.config.MmuSDKFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by jhenxu on 15/5/6.
 */
public abstract class MojiBanner extends RelativeLayout implements FeedController.IncubatedListener,View.OnClickListener {
    private String slot;
    private ImageView imageView;
    private FeedController mController;
    private FeedController.MMFeed product;
    private Activity activity;
    public MojiBanner(Context context) {
        super(context);
    }

    public boolean init(Activity activity,String slot) {
        this.slot = slot;
        this.activity = activity;
        if(imageView == null){
            imageView = new ImageView(getContext());
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            addView(imageView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        }

        setVisibility(View.GONE);
        FeedProperties prop = new FeedProperties(slot);
        MmuSDK mmuSDK = MmuSDKFactory.getMmuSDK();

        mController = FeedProperties.getMmuController();
       if (mController != null) {
            mController.getProduct(slot);
            mController.setIncubatedListener(this);//设置Feed初始化过程的监听
            return true;
        }
        mmuSDK.attach(prop);

        return false;
    }

    @Override
    public void onComplete(int status, String s) {
        if (1 == status){
            product = mController.getProduct(slot);
            if(product != null && product.getPromoters() != null && product.getPromoters().size() >0){
                MMPromoter promoter = product.getPromoters().get(0);
                if(!TextUtils.isEmpty(promoter.getImg())){
                    onImageLoad(promoter.getImg(),imageView);
                    setVisibility(View.VISIBLE);
                    product.reportImpression(activity,product.getPromoters().get(0));
                    setOnClickListener(this);
                }
            }
        }
    }

    @Override
    public void onClick() {
    }

    @Override
    public void onClick(View v) {
        product.clickOnPromoter(activity,product.getPromoters().get(0));
    }


    public int[] getSize(){
        try {
            if(product != null) {
                Field f1 = FeedController.MMFeed.class.getDeclaredField("feed");
                f1.setAccessible(true);
                Object feed = f1.get(product);
                Method getDataService = feed.getClass().getDeclaredMethod("getDataService", Context.class);
                getDataService.setAccessible(true);
                Object dataService = getDataService.invoke(feed, activity);
                Method getEntity = dataService.getClass().getDeclaredMethod("getEntity");
                getEntity.setAccessible(true);
                Object entity = getEntity.invoke(dataService);
                Field lz = entity.getClass().getDeclaredField("landing_size");
                lz.setAccessible(true);
                String size = (String) lz.get(entity);
                if (!TextUtils.isEmpty(size)) {
                    String[] xes = size.split("x");
                    if (xes != null && xes.length == 2) {
                        int[] ss = new int[2];
                        ss[0] = Integer.parseInt(xes[0]);
                        ss[1] = Integer.parseInt(xes[1]);
                        return ss;
                    }
                }
            }
        }catch (Exception e){
        }
        return null;
    }

    public abstract void onImageLoad(String url,ImageView imageView);
}
